@extends('layouts.layout')

@section('header')

    <title>地址</title>

    <link rel="stylesheet" type="text/css" href="{{URL::asset('static/css/address.css')}}"/>

@endsection

@section('content')

    @isset($datas)

        @foreach ($datas as $data)
        <a class="address shadow" data-id="{{$data->id}}" href="javascript:;">
        	<div class="user_info">
        		<div class="top">
        			<div class="one">
                        <div style="width:.3rem;margin:0 auto;text-align:center;">{{ $data->name }}</div>
        			</div>
        			<div style="flex: 1 1 auto;">
        				<p><i>{{ $data->name }}</i><span>{{ $data->mobile }}</span></p>
        				<div class="detailed">{{ $data->area }},{{ $data->address }}</div>
        			</div>
        			<div class="to_add_address" data-info="{{$data}}">
        				<img src="{{URL::asset('static/img/change.png')}}" style="height: .28rem;width: auto;">
        			</div>
        		</div>

        		<div class="default">

        			<label for="radio{{ $data->id }}" class="labelClick">
        				<img src="{{URL::asset('static/img/radio.png')}}" class="nosel" alt="">
        				<img src="{{URL::asset('static/img/radioSel.png')}}" class="sel" alt="">

                        @if($data->is_default == 1)
                            <input type="radio" data-id="{{ $data->id }}" id="radio{{ $data->id }}" class="radio_type" checked="checked" name="address">
                        @else
                            <input type="radio" data-id="{{ $data->id }}" id="radio{{ $data->id }}" class="radio_type" name="address">
                        @endif

                        默认地址
        			</label>

        			<div class="del_addres" data-id="{{$data->id}}">删除</div>
        		</div>
        	</div>
        </a>

        @endforeach
    @endisset

        <a href="javascript:;" class="add_address">添加地址</a>

@endsection

@section('script')
    <script type="text/javascript">
        $(function(){
            // 去修改地址
            $(document).on('click','.to_add_address',function(){
                var data = $(this).attr("data-info")
                sessionStorage.setItem('changeAddress',data)
                sessionStorage.setItem("addressType","change")
                window.location.href = "/user/addressAdd"
                return false;
            })

            // 从我的页面进来
            if(document.referrer.search("my") != -1){
                $(document).on('click','.address',function(){
                    return false
                })
            }else{
                // 选择地址   订单页面进来的
                $(document).on('click','.address',function(){
                    var name = $(this).find('i').html()
                    var mobile = $(this).find('span').html()
                    var area = $(this).find('.detailed').html()
                    var arr = {}
                    arr['address_id'] = $(this).attr('data-id');
                    arr['name'] = name;
                    arr['mobile'] = mobile;
                    arr['address'] = area;
                    sessionStorage.setItem('address',JSON.stringify(arr))
                    window.history.go(-1)
                })
            }


            $(".add_address").click(function(){
                sessionStorage.removeItem('address')
                sessionStorage.setItem("addressType","add")
                window.location.href = "/user/addressAdd"
            })
        	// 删除地址
        	$(".del_addres").click(function(e){
                var id = $(this).attr('data-id');

                layer.open({
                    content: '确定要删除吗？'
                    , btn: ['确定', '取消']
                    , yes: function (index) {
                        commonAjax({type:"delete",url:'/user/deleteAddress/'+id},function(msg){
                            window.location.reload()
                        })
                    }
                });
                
                e.stopPropagation();
        	})
            // 设为默认地址
            $(".radio_type").click(function(e){
                if($(this).is(":checked")){
                    return false;
                }
                var data = {};
                data['id'] = $(this).data('id');
                data['is_default'] =1;

                commonAjax({type:"post",url:'/user/saveAddress',data},function(msg){
                    $("input[name = 'address']").siblings('.sel').hide()
                    $("input[name = 'address']").siblings('.nosel').show()
                    $("input[name = 'address']:checked").siblings('.sel').show()
                    $("input[name = 'address']:checked").siblings('.nosel').hide()
                    window.location.reload()
                })
                e.stopPropagation();
            })
            $("input[name = 'address']").siblings('.sel').hide()
            $("input[name = 'address']").siblings('.nosel').show()
            $("input[name = 'address']:checked").siblings('.sel').show()
            $("input[name = 'address']:checked").siblings('.nosel').hide()
            $('.labelClick').click(function(){
                $(this).find("input").trigger('click')
                $("input[name = 'address']").siblings('.sel').hide()
                $("input[name = 'address']").siblings('.nosel').show()
                $("input[name = 'address']:checked").siblings('.sel').show()
                $("input[name = 'address']:checked").siblings('.nosel').hide()
                return false
            })

            window.onpageshow = function(event) {
                if (event.persisted || window.performance &&
                    window.performance.navigation.type == 2) {
                    window.location.reload();
                }
            }

        })
    </script>

@endsection
